﻿using System;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class CodelistModule : BaseModule
    {
        public CodelistModule(IMetaProvider provider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(provider, pdfDocument, fontsProvider, cfg)
        {
            //get root of codelists
            HierarchyNode maintenableMeta = metaProvider.GetRootNode();
            bool nodetail = false;
            // Codelist maintenable info ?
            bool empty = metaProvider.GetAnnotations(maintenableMeta).Count == 0;
            if (string.IsNullOrEmpty(maintenableMeta.Descr) && empty)
            {
                nodetail = true;
            }
            else
            {
                ProvideMaintenableInfo(maintenableMeta);
            }

            // Produce flat hierarchy
            // For all codes
            int indexNo = 1;
            // Create list table
            PdfPTable table = CreateIndentedListTable2("Code");
            if (nodetail)
            {
                AddAnchoredTitle2(table, maintenableMeta.Id , maintenableMeta.Name, metaProvider.FullIdKey);
            }
            ProduceIndentedList(table, maintenableMeta, 1, string.Empty, ref indexNo);
            AddLeading(table, 20f);

            if (maintenableMeta.HasDetail)
            {
                table = CreatePropertyTable();
                PropertySubTitle(table, "Codes détaillés");
                Tables.Add(new PdfBookPTable(table));
                TableParagraph();


                // Say: Additional detail of some codes

                // For each code that has additional indo
                //    Tell code and name as subTitle
                //    Tell Description if any
                // Tell annotations (if any)

                ProduceHierarchyDetail(table, maintenableMeta, 1, string.Empty, ref indexNo);
            }
        }

        private void AddAnchoredTitle2(PdfPTable table, string id, string text, string anchorName)
        {
            Phrase ph = new Phrase(id + ": " + text, BookFonts.FontLevelOne);
            Anchor target = new Anchor(ph);
            target.Name = anchorName;
            PdfPCell cell = new PdfPCell(target);
            cell.BackgroundColor = new BaseColor(BookFonts.COLOR_TABLE_HEAD);
          //  cell.BackgroundColor = new BaseColor(BookFonts.COLOR_TABLE_ALTERNED);
            cell.MinimumHeight = 12f;
            cell.PaddingBottom = 3;
            cell.Border = Rectangle.NO_BORDER;
            cell.Colspan = 2;
            table.Rows.Insert(0, new PdfPRow(new PdfPCell[] { cell, null }));

            //ph = new Phrase(text, BookFonts.FontTableHead);
            //cell = new PdfPCell(ph);
            //cell.BackgroundColor = new BaseColor(BookFonts.COLOR_TABLE_HEAD);
            //cell.Border = Rectangle.NO_BORDER;
            //Table.AddCell(cell);
        }

        private void ProvideMaintenableInfo(HierarchyNode rootNode)
        {
            PropertyTable pt = new PropertyTable(metaProvider, BookFonts, doc);
            pt.AddAnchoredTitle2(rootNode.Id , rootNode.Name, metaProvider.FullIdKey);
            if (string.IsNullOrEmpty(rootNode.Descr) == false)
            {
                pt.AddLeading(5f);
                pt.AddProperty("Description", rootNode.Descr);
                pt.AddLeading(12f);
            }

            pt.AddAnnotations(metaProvider.GetAnnotations(rootNode));
            Tables.Add(new PdfBookPTable(pt.Table));


            //// Module detail
            //if (string.IsNullOrEmpty(rootNode.Descr) == false)
            //{
            //    PdfPTable table = CreatePropertyTable();
            //    MakeRowVarDetail(table, "Description", rootNode.Descr, false);
            //    Tables.Add(table);
            //}
            //// Any annotations
            //CreateAnnotationTable(metaProvider.GetAnnotations(rootNode));
        }

        protected void ProduceHierarchyDetail(PdfPTable table, HierarchyNode rootNode, int level, string parentCode, ref int indexNo)
        {
            // Iterates items
            foreach (HierarchyNode childNode in rootNode.Nodes)
            {
                if (childNode.HasDetail)
                {
                    PropertySubTitle(table, "Code", childNode.Id);
                  //  MakeRowVarDetail(table, "Code", childNode.Id, false);
                    MakeRowVarDetail(table, "Name", childNode.Name, true);
                    if (string.IsNullOrEmpty(childNode.Descr) == false)
                        MakeRowVarDetail(table, "Description", childNode.Descr, true);

                    // Annotations
                    List<HierarchyNode> annoList = metaProvider.GetAnnotations(childNode);
                    foreach (HierarchyNode ndAnnot in annoList)
                    {
                        string typeTitle = string.IsNullOrEmpty(ndAnnot.Descr) ? ndAnnot.Id : ndAnnot.Id + " : " + ndAnnot.Descr;

                        MakeRowVarDetail(table, typeTitle, ndAnnot.Name, true);
                    }
                }

                // Create lower level Table list
                ProduceHierarchyDetail(table, childNode, level + 1, childNode.Id, ref indexNo);
            }

        }


    }
}
